package com.youlai.boot.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.common.util.DateUtils;
import com.youlai.boot.system.converter.WxKfRecordConverter;
import com.youlai.boot.system.mapper.WxKfRecordMapper;
import com.youlai.boot.system.model.bo.WxKfRecordBO;
import com.youlai.boot.system.model.entity.WxKfRecord;
import com.youlai.boot.system.model.form.WxKfRecordForm;
import com.youlai.boot.system.model.query.WxKfRecordPageQuery;
import com.youlai.boot.system.model.vo.WxKfRecordPageVO;
import com.youlai.boot.system.service.WxKfRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理记录表服务实现类
 *
 * @author Ray Hao
 * @since 2025-09-01
 */
@Service
@RequiredArgsConstructor
public class WxKfRecordServiceImpl extends ServiceImpl<WxKfRecordMapper, WxKfRecord> implements WxKfRecordService {

    private final WxKfRecordConverter wxKfRecordConverter;

    /**
     * 获取护理记录表分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<WxKfRecordPageVO>} 护理记录表分页列表
     */
    @Override
    public IPage<WxKfRecordPageVO> listPagedWxKfRecords(WxKfRecordPageQuery queryParams) {

        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<WxKfRecordBO> page = new Page<>(pageNum, pageSize);

        if (queryParams.getServiceDate() != null && queryParams.getServiceDate().length == 2) {
            queryParams.setStartTime(queryParams.getServiceDate()[0]);
            queryParams.setEndTime(queryParams.getServiceDate()[1]);
        }

        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");

        // 查询数据
        Page<WxKfRecordBO> boPage = this.baseMapper.listPagedWxKfRecords(page, queryParams);

        // 实体转换
        return wxKfRecordConverter.toPageVo(boPage);
    }

    /**
     * 获取护理记录表表单数据
     *
     * @param id 护理记录表ID
     * @return
     */
    @Override
    public WxKfRecordForm getWxKfRecordFormData(Long id) {
        WxKfRecord entity = this.getById(id);
        return wxKfRecordConverter.toForm(entity);
    }

    /**
     * 新增护理记录表
     *
     * @param formData 护理记录表表单对象
     * @return
     */
    @Override
    public boolean saveWxKfRecord(WxKfRecordForm formData) {
        // 实体转换 form->entity
        WxKfRecord entity = wxKfRecordConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新护理记录表
     *
     * @param id       护理记录表ID
     * @param formData 护理记录表表单对象
     * @return
     */
    @Override
    public boolean updateWxKfRecord(Long id, WxKfRecordForm formData) {
        WxKfRecord entity = wxKfRecordConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除护理记录表
     *
     * @param ids 护理记录表ID，多个以英文逗号(,)分割
     * @return true|false
     */
    @Override
    public boolean deleteWxKfRecords(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的护理记录表数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
